home *** CD-ROM | disk | FTP | other *** search
/ Gamers Delight 2 / Gamers Delight 2.iso / Aminet / game / board / GNUChess4_0_58.lha / gnuchess-4.0 / src / new / game.c < prev    next >
C/C++ Source or Header  |  1992-08-26  |  10KB  |  425 lines

  1. /*
  2.  * postprint.c - C source for GNU CHESS
  3.  *
  4.  * Copyright (c) 1988,1989,1990 John Stanback
  5.  * Copyright (c) 1992 Free Software Foundation
  6.  *
  7.  * This file is part of GNU CHESS.
  8.  *
  9.  * GNU Chess is free software; you can redistribute it and/or modify
  10.  * it under the terms of the GNU General Public License as published by
  11.  * the Free Software Foundation; either version 2, or (at your option)
  12.  * any later version.
  13.  *
  14.  * GNU Chess is distributed in the hope that it will be useful,
  15.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  17.  * GNU General Public License for more details.
  18.  *
  19.  * You should have received a copy of the GNU General Public License
  20.  * along with GNU Chess; see the file COPYING.  If not, write to
  21.  * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  22.  */
  23. #include <stdio.h>
  24. #include "gnuchess.h"
  25. #ifdef MSDOS
  26. #include <stdlib.h>
  27. #include <string.h>
  28. #include <time.h>
  29. #define RWA_ACC "r+b"
  30. #define WA_ACC "w+b"
  31. #else
  32. #define RWA_ACC "r+"
  33. #define WA_ACC "w+"
  34. #include <sys/param.h>
  35. #include <sys/types.h>
  36. #endif /* MSDOS */
  37. FILE *fd;
  38.  
  39. #define truescore 0x0001
  40. #define lowerbound 0x0002
  41. #define upperbound 0x0004
  42. #define kingcastle 0x0008
  43. #define queencastle 0x0010
  44. const short otherside[3] =
  45. {black, white, neutral};
  46. const short Stboard[64] =
  47. {rook, knight, bishop, queen, king, bishop, knight, rook,
  48.  pawn, pawn, pawn, pawn, pawn, pawn, pawn, pawn,
  49.  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  50.  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  51.  pawn, pawn, pawn, pawn, pawn, pawn, pawn, pawn,
  52.  rook, knight, bishop, queen, king, bishop, knight, rook};
  53. const short Stcolor[64] =
  54. {white, white, white, white, white, white, white, white,
  55.  white, white, white, white, white, white, white, white,
  56.  neutral, neutral, neutral, neutral, neutral, neutral, neutral, neutral,
  57.  neutral, neutral, neutral, neutral, neutral, neutral, neutral, neutral,
  58.  neutral, neutral, neutral, neutral, neutral, neutral, neutral, neutral,
  59.  neutral, neutral, neutral, neutral, neutral, neutral, neutral, neutral,
  60.  black, black, black, black, black, black, black, black,
  61.  black, black, black, black, black, black, black, black};
  62.  
  63. struct GameRec GameList[512];
  64. char mvstr[4][6];
  65. long i, j;
  66. int nr;
  67. short int ep;
  68. int r, c;
  69. char line[128];
  70. char *l;
  71. short int board[64];
  72. short int color[64];
  73. short int GameCnt;
  74. int from, to;
  75. char *InPtr;
  76.  
  77. void
  78. skip ()
  79. {
  80.   while (*InPtr != ' ')
  81.     InPtr++;
  82.   while (*InPtr == ' ')
  83.     InPtr++;
  84. }
  85. void
  86. skipb ()
  87. {
  88.   while (*InPtr == ' ')
  89.     InPtr++;
  90. }
  91. int
  92. parser (char *f, int side, unsigned short *flags)
  93. {
  94.   int c1, r1, c2, r2;
  95.  
  96.   *flags = 0;
  97.  
  98.   if (f[4] == 'o')
  99.     if (side == black)
  100.       return 0x3C3A;
  101.     else
  102.       return 0x0402;
  103.   else if (f[0] == 'o')
  104.     if (side == black)
  105.       return 0x3C3E;
  106.     else
  107.       return 0x0406;
  108.   else
  109.     {
  110.       c1 = f[0] - 'a';
  111.       r1 = f[1] - '1';
  112.       c2 = f[2] - 'a';
  113.       r2 = f[3] - '1';
  114.       if (f[4] != ' ')
  115.     {
  116.       /* promotion */
  117.       for (i = 0; i < 7; i++)
  118.         if (f[4] == Qxx[i])
  119.           {
  120.         *flags = i | promote;
  121.         break;
  122.           }
  123.     }
  124.       return (locn (r1, c1) << 8) | locn (r2, c2);
  125.     }
  126.   return (0);
  127. }
  128.  
  129. void
  130. algbr (short int f, short int t, short int flag)
  131.  
  132.  
  133. /*
  134.  * Generate move strings in different formats.
  135.  */
  136.  
  137. {
  138.   int m3p;
  139.  
  140.   if (f != t)
  141.     {
  142.       /* algebraic notation */
  143.       mvstr[0][0] = Cxx[column (f)];
  144.       mvstr[0][1] = Rxx[row (f)];
  145.       mvstr[0][2] = Cxx[column (t)];
  146.       mvstr[0][3] = Rxx[row (t)];
  147.       mvstr[0][4] = mvstr[3][0] = '\0';
  148.       if (((mvstr[1][0] = Pxx[board[f]]) == 'P') || (flag & promote))
  149.     {
  150.       if (mvstr[0][0] == mvstr[0][2])    /* pawn did not eat */
  151.         {
  152.           mvstr[2][0] = mvstr[1][0] = mvstr[0][2];    /* to column */
  153.           mvstr[2][1] = mvstr[1][1] = mvstr[0][3];    /* to row */
  154.           m3p = 2;
  155.         }
  156.       else
  157.         /* pawn ate */
  158.         {
  159.           mvstr[2][0] = mvstr[1][0] = mvstr[0][0];    /* column */
  160.           mvstr[2][1] = mvstr[1][1] = mvstr[0][2];    /* to column */
  161.           mvstr[2][2] = mvstr[0][3];
  162.           m3p = 3;        /* to row */
  163.         }
  164.       if (flag & promote)
  165.         {
  166.           mvstr[0][4] = mvstr[1][2] = mvstr[2][m3p] = Qxx[flag & pmask];
  167.           mvstr[1][3] = mvstr[2][m3p + 1] = mvstr[0][5] = '\0';
  168. #ifdef CHESSTOOL
  169.           mvstr[3][0] = mvstr[0][0];    /* Allow e7e8 for chesstool */
  170.           mvstr[3][1] = mvstr[0][1];
  171.           mvstr[3][2] = mvstr[0][2];
  172.           mvstr[3][3] = mvstr[0][3];
  173.           mvstr[3][4] = '\0';
  174. #endif
  175.         }
  176.       mvstr[2][m3p] = mvstr[1][2] = '\0';
  177.     }
  178.       else
  179.     /* not a pawn */
  180.     {
  181.       mvstr[2][0] = mvstr[1][0];
  182.       mvstr[2][1] = mvstr[0][1];
  183.       mvstr[2][2] = mvstr[1][1] = mvstr[0][2];    /* to column */
  184.       mvstr[2][3] = mvstr[1][2] = mvstr[0][3];    /* to row */
  185.       mvstr[2][4] = mvstr[1][3] = '\0';
  186.       strcpy (mvstr[3], mvstr[2]);
  187.       mvstr[3][1] = mvstr[0][0];
  188.       if (flag & cstlmask)
  189.         {
  190.           if (t > f)
  191.         {
  192.           strcpy (mvstr[1], "o-o");
  193.           strcpy (mvstr[2], "O-O");
  194.         }
  195.           else
  196.         {
  197.           strcpy (mvstr[1], "o-o-o");
  198.           strcpy (mvstr[2], "O-O-O");
  199.         }
  200.         }
  201.     }
  202.     }
  203.   else
  204.     mvstr[0][0] = mvstr[1][0] = mvstr[2][0] = mvstr[3][0] = '\0';
  205. }
  206.  
  207. GetGame ()
  208. {
  209.   char fb[256];
  210.   unsigned short flags;
  211.  
  212.   fgets (fb, 256, fd);
  213.   fgets (fb, 256, fd);
  214.   while (fgets (fb, 256, fd))
  215.     {
  216.       struct GameRec *g;
  217.       int side = white;
  218.  
  219.       side = otherside[side];
  220.       if (fb[0] == '\n')
  221.     return;
  222.       ++GameCnt;
  223.       InPtr = fb;
  224.       skipb ();
  225.       g = &GameList[GameCnt];
  226.       g->gmove = parser (InPtr, side, &flags);
  227.       skip ();
  228.       g->score = atoi (InPtr);
  229.       skip ();
  230.       g->depth = atoi (InPtr);
  231.       skip ();
  232.       g->nodes = atoi (InPtr);
  233.       skip ();
  234.       g->time = atoi (InPtr);
  235.       g->flags = flags;
  236.       skip ();
  237.       ++GameCnt;
  238.       g = &GameList[GameCnt];
  239.       g->gmove = parser (InPtr, side, &flags);
  240.       skip ();
  241.       g->score = atoi (InPtr);
  242.       skip ();
  243.       g->depth = atoi (InPtr);
  244.       skip ();
  245.       g->nodes = atoi (InPtr);
  246.       skip ();
  247.       g->time = atoi (InPtr);
  248.       g->flags = flags;
  249.  
  250.     }
  251. }
  252. short int xside, side;
  253. int
  254. getboard (int mvno)
  255.  
  256. {
  257.   register short int f, t;
  258.   char opening[80];
  259.   short int rf, rt;
  260.   short int c, doit;
  261.   unsigned short mv;
  262.  
  263.  
  264.   /* now update the board and hash values */
  265.  
  266.   /*
  267.    * should really check the moves as we do this, but???
  268.    */
  269.   mv = GameList[mvno].gmove;
  270.   f = mv >> 8 & 0x7F;
  271.   t = mv & 0xFF;
  272.   /* can only capture other side */
  273.   if (board[t] != no_piece)
  274.     {
  275.       if (color[t] != xside)
  276.     {
  277.       algbr (f, t, 0);
  278.       printf ("Illegal move - %d %s \n", mvno, mvstr);
  279.     }
  280.     }
  281.   /* there must be a piece to move */
  282.   if (board[f] == no_piece || color[f] != side)
  283.     {
  284.       algbr (f, t, 0);
  285.       printf ("Illegal move + %d %s \n", mvno, mvstr);
  286.     }
  287.   /* is it EnPassant */
  288.   if (board[f] == pawn && board[t] == no_piece)
  289.     {
  290.       if ((row (f) == 4 && row (t) == 3) || (row (f) == 5 && row (t) == 6))
  291.     {
  292.       if ((column (t) == column (f) + 1)
  293.           || (column (t) == column (f) - 1))
  294.         {
  295.           ep = t + ((t > f) ? -8 : 8);
  296.           if (board[ep] == pawn && color[ep] == xside)
  297.         {
  298.           board[ep] = no_piece;
  299.           color[ep] = neutral;
  300.         }
  301.         }
  302.     }
  303.     }
  304.   board[t] = board[f];
  305.   color[t] = color[f];
  306.   color[f] = neutral;
  307.   board[f] = no_piece;
  308.   /* castle moves */
  309.   if ((board[t] == king) & ((mv == BLACKCASTLE) || (mv == WHITECASTLE) || (mv == LONGBLACKCASTLE) || (mv == LONGWHITECASTLE)))
  310.     {
  311.  
  312.       if (t > f)
  313.     {
  314.       rf = f + 3;
  315.       rt = t - 1;
  316.     }
  317.       else
  318.     {
  319.       rf = f - 4;
  320.       rt = t + 1;
  321.     }
  322.       board[rt] = rook;
  323.       color[rt] = side;
  324.       board[rf] = no_piece;
  325.       color[rf] = neutral;
  326.     }
  327.   else if (GameList[i].flags & promote)
  328.  
  329.     board[t] = GameList[i].flags & pmask;
  330.   xside = side;
  331.   side = otherside[side];
  332. }
  333.  
  334. int
  335. main (int argc, char **argv)
  336. {
  337.   int from, to;
  338.   int f = 0;
  339.   unsigned short int mv;
  340.   int start, end;
  341.  
  342.   if (argc > 4 || argc < 2)
  343.     {
  344.       printf ("Usage: game file [start [end] ] \n");
  345.       exit (1);
  346.     }
  347.   start = end = 0;
  348.   if (argc > 2)
  349.     start = (atoi (argv[2]) * 2) - 1;
  350.   if (argc == 4)
  351.     end = (atoi (argv[3]) * 2) - 1;
  352.   side = white;
  353.   xside = black;
  354.   for (i = 0; i < 64; i++)
  355.     {
  356.       board[i] = Stboard[i];
  357.       color[i] = Stcolor[i];
  358.     }
  359.   i = 1;
  360.   if ((fd = fopen (argv[1], RWA_ACC)) == NULL)
  361.     exit (1);
  362.   printf ("/V 11 72 mul def /L 60 def\n");
  363.   GetGame ();
  364.   if (!start || start < 1 || start > GameCnt)
  365.     start = 1;
  366.   if (!end || end > GameCnt || end < 1)
  367.     end = GameCnt;
  368.   for (i = 1; i < end; i++)
  369.     {
  370.       getboard (i);
  371.       if (i < start)
  372.     continue;
  373.       nr++;
  374.       if (nr == 19)
  375.     {
  376.       nr = 1;
  377.       printf ("showpage\n/V 11 72 mul def\n");
  378.       printf ("/L 60 def\n");
  379.       f = 0;
  380.     }
  381.       /* now process this entry */
  382.       strcpy (line, "C ('#[");
  383.       for (r = 0; r < 8; r++)
  384.     {
  385.       l = line + 6 + (7 - r) * 9;
  386.       for (c = 0; c < 8; c++)
  387.         {
  388.           if (color[r * 8 + c] == black)
  389.         *l++ = Qxx[board[r * 8 + c]];
  390.           else
  391.         *l++ = Pxx[board[r * 8 + c]];
  392.         }
  393.       *l++ = ';';
  394.     }
  395.       l--;
  396.       line[79] = '\0';
  397.       strcat (line, "]') show");
  398.       /* decode flags */
  399.       printf ("L V moveto\n");
  400.       mv = GameList[i].gmove;
  401.       from = mv >> 8 & 0x7F;
  402.       to = mv & 0x7F;
  403.       algbr (from, to, 0);
  404.       if (i % 2)
  405.     printf ("R (%d %s score %d time %d", (i + 1) / 2, mvstr, GameList[i].score, GameList[i].time);
  406.       else
  407.     printf ("R (%d  ... %s score %d time %d", (i + 1) / 2, mvstr, GameList[i].score, GameList[i].time);
  408.       printf (") show\n");
  409.       printf ("L  V 100 sub moveto\n");
  410.       printf ("%s\n", line);
  411.       f++;
  412.       if (f == 3)
  413.     {
  414.       printf ("/V V 120 sub def /L 60 def\n");
  415.       f = 0;
  416.     }
  417.       else
  418.     printf ("/L 160 L add def\n");
  419.     }
  420.  
  421.   if (nr)
  422.     printf ("showpage\n");
  423.   exit (0);
  424. }
  425.